package com.cruise.game.controller.agent;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.constants.CommonConstants;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.vo.common.PageResult;
import com.cruise.game.model.vo.order.OrderDetailVO;
import com.cruise.game.model.vo.order.OrderListVO;
import com.cruise.game.service.order.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 代理订单控制器
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "代理订单相关接口")
@RestController
@RequestMapping("/api/agent/orders")
@PreAuthorize("hasAnyRole('ROLE_AGENT', 'ROLE_ADMIN')")
public class AgentOrderController extends BaseController {

    @Resource
    private OrderService orderService;

    /**
     * 获取待接单列表
     */
    @ApiOperation("获取待接单列表")
    @GetMapping("/pending")
    public ApiResult<PageResult<OrderListVO>> getPendingOrders(
            @ApiParam(value = "游戏分类ID", required = false)
            @RequestParam(value = "gameCategoryId", required = false) Long gameCategoryId,
            
            @ApiParam(value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            
            @ApiParam(value = "每页条数", required = false, defaultValue = "10")
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        
        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }
        
        return ApiResult.success(orderService.getPendingOrders(gameCategoryId, page, pageSize));
    }

    /**
     * 接单
     */
    @ApiOperation("接单")
    @PostMapping("/{orderId}/accept")
    public ApiResult<Boolean> acceptOrder(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId) {
        return ApiResult.success("已接单", orderService.acceptOrder(orderId, getCurrentUserId()));
    }

    /**
     * 开始服务
     */
    @ApiOperation("开始服务")
    @PostMapping("/{orderId}/start")
    public ApiResult<Boolean> startService(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId) {
        return ApiResult.success("服务已开始", orderService.startService(orderId, getCurrentUserId()));
    }

    /**
     * 完成服务
     */
    @ApiOperation("完成服务")
    @PostMapping("/{orderId}/complete")
    public ApiResult<Boolean> completeService(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId,
            
            @ApiParam(value = "完成截图URL", required = true)
            @RequestBody Map<String, String> param) {
        String completionImageUrl = param.get("completionImageUrl");
        return ApiResult.success("已完成服务，等待用户确认", 
                orderService.completeService(orderId, completionImageUrl, getCurrentUserId()));
    }

    /**
     * 获取代理订单列表
     */
    @ApiOperation("获取代理订单列表")
    @GetMapping
    public ApiResult<PageResult<OrderListVO>> getAgentOrderList(
            @ApiParam(value = "订单状态", required = false, defaultValue = "all")
            @RequestParam(value = "status", required = false, defaultValue = "all") String status,
            
            @ApiParam(value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            
            @ApiParam(value = "每页条数", required = false, defaultValue = "10")
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        
        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }
        
        return ApiResult.success(orderService.getAgentOrderList(getCurrentUserId(), status, page, pageSize));
    }

    /**
     * 获取代理订单详情
     */
    @ApiOperation("获取代理订单详情")
    @GetMapping("/{orderId}")
    public ApiResult<OrderDetailVO> getAgentOrderDetail(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId) {
        return ApiResult.success(orderService.getAgentOrderDetail(orderId, getCurrentUserId()));
    }
}
